package com.ang.quartz.springboot.config;

import com.ang.quartz.normal.pojo.Data;
import com.ang.quartz.springboot.job.HiJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ssang
 * @date 2022-01-07 9:45
 */
@Configuration
public class QuartzConfig {
    @Bean("jobDetail")
    public JobDetail getJobDetail(){
        JobDetail jobDetail = JobBuilder.newJob(HiJob.class)
                .withIdentity("myJob","myJobGroup")
                //传参方式1：传字符串
                .usingJobData("job_param","job_param_val")
                //表示任务完成之后是否依然保留到数据库，默认false
                .storeDurably(true)
                .build();
        //传参方式2：传递对象
        Data data = new Data("1","我是JobDetail参数");
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        jobDataMap.put("job_data",data);

        return jobDetail;
    }

    @Bean
    public Trigger getTrigger(JobDetail jobDetail){
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("myTrigger","myTriggerGroup")
                //传参方式1：传字符串
                .usingJobData("job_trigger_param","123")
                .forJob(jobDetail)
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever())
//                .withSchedule(CronScheduleBuilder.cronSchedule(Utils.getCron(LocalDateTime.of(2021,5,31,14,41,00))))
//                .withSchedule(CronScheduleBuilder.cronSchedule(Utils.getCronNow()))
                .build();
        //传参方式2：传递对象
        JobDataMap jobDataMap1 = trigger.getJobDataMap();
        Data data1 = new Data("2","我是trigger参数");
        jobDataMap1.put("trigger_data",data1);

        return trigger;
    }
}
